package com.woniuxy.tfss.web.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.tfss.entity.Admin;
import com.woniuxy.tfss.entity.ResponseEntity;
import com.woniuxy.tfss.entity.Rider;
import com.woniuxy.tfss.service.RiderService;
import com.woniuxy.tfss.utils.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Controller("adminRiderController")
@RequestMapping("admin/rider")
@CrossOrigin
@Slf4j
public class RiderController {
    @Autowired
    private RiderService riderService;

    /**
     * 查询所有骑手
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity<List<Rider>> list(){
        List<Rider> riders=riderService.list();
        return new ResponseEntity<>(riders);
    }

    /**
     * 多条件分页查询骑手
     * @param account
     * @param credit1
     * @param credit2
     * @param workStatus
     * @param status
     * @param page
     * @return
     */
    @GetMapping("listByPage")
    @ResponseBody
    public ResponseEntity<Page<Rider>> listByPage(String account, String credit1,String credit2,String workStatus, String status, Integer page){
        QueryWrapper<Rider> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(account)){
            wrapper.like("account",account);
        }else if(!StringUtils.isEmpty(status)&& !"0".equals(status)){
            wrapper.eq("status", status);
        }else if(!StringUtils.isEmpty(workStatus)&& !"0".equals(workStatus)){
            wrapper.eq("workStatus", workStatus);
        }
        wrapper.between("credit",credit1,credit2);
        Page riderPage=new Page(page==null?1:page,3);
        return new ResponseEntity<>(riderService.page(riderPage,wrapper));
    }

    /**
     * 冻结骑手
     * @param id
     * @return
     */
    @GetMapping("changeStatus")
    @ResponseBody
    public  ResponseEntity<Void> changeStatus(Integer id){
        Rider rider=riderService.getById(id);
        QueryWrapper<Rider> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        if(Rider.NORMAL.equals(rider.getStatus())){
            rider.setStatus(Admin.LOCKED);
        }else{
            rider.setStatus(Admin.NOMAL);
        }
        riderService.update(rider,wrapper);
        return ResponseEntity.SUCCESS;
    }
}
